import { defineStore } from 'pinia'
import Api from '@/api'
import Dict from '@/Utils/dict'

export const useDealerDataStore = defineStore('dealerData', {
  persist: {
    enabled: true,
  },
  state: () => {
    return {
      // 奖金构成
      dealerPriceDetailPie: {
        title: '',
        info: [],
      },
      // 整网业绩饼状图
      dealerTotalWebsitePie: {
        title: '',
        info: [],
      },
      // 奖金整网业绩占比
      dealerScaleInfo: {
        title: '',
        info: [],
      },
      // 整网业绩
      dealerDistributorZwall: {
        title: '',
        info: {
          zw: 0,
          currency: '',
        },
      },
      // 升级助手
      // dealerDisupgradeinfo: {
      //   title: '',
      //   info: {
      //     plan: [],
      //     upgradeinfo: {
      //       target_level: '',
      //       cur_level: '',
      //       target_level_value: '',
      //     },
      //     isplan: true,
      //   },
      // },
      //升级助手方案
      // dealerGetplaninfo: {
      //   info: {
      //     planinfo: [],
      //   },
      //   title: '',
      // },
      //新人
      dealerGrowthTrend: {
        title: '',
        x:'',
        y:'',
        info: [],
      },
      //整网业绩增涨趋势
      dealerNetworkGrowthTrend: {
        title: '',
        info: {
          bonus: '',
          bonusy: '',
          performance: '',
          performancey: '',
          list: [],
        },
      },
      // 经销商个人信息
      distributorBasicInfo: {
        title: '',
        info: {
          country: '',
          countrydesc: '',
          distributorid: '',
          distributoriddesc: '',
          distributorname: '',
          distributornamedesc: '',
          framelevel: '',
          frameleveldesc: '',
          level: '',
          leveldesc: '',
        },
      },

      // 网络情况
      networkOveRview: {
        info: {
          activeDistributorNumber: '',
          activeDistributorNumberDesc: '',
          distributorNumber: '',
          distributorNumberesc: '',
          newDistributorNumber: '',
          newDistributorNumberDesc: '',
          validDistributorNumber: '',
          validDistributorNumberDesc: '',
        },
      },
      // 徽章信息
      dealerBadgeInfo: {
        info: null,
        title: null,
      },
      // 网络
      dealerNetWork: {
        info: {
          network: [],
          personal: [],
          personal_title: '',
        },
        title: '',
        y: ''
      },
      // 版本
      version: {},
    }
  },

  actions: {
    clearVersion() {
      this.version = {}
    },
    getAll() {
      // Dict对象获取dealerData,然后key和value对调,后端那个key为键值,咱们的方法为值
      const overturnDict = Object.keys(Dict.dealerData).reduce(
        (obj: any, key: any) => {
          obj[Dict.dealerData[key]] = key
          return obj
        },
        {}
      )

      Api.dealerBasicInterfaceversioninfo().then((res: any) => {
        if (res.code != 0) {
          return false
        }
        const newVersion = res.data
        // 获取到版本号newVersion和本地版本号version对比,如果值不一样,获取到key
        const keys = Object.keys(newVersion).filter((item: any) => {
          if (!item) {
            return false
          }
          // 本地version和newVersion对比,如果值不一样,就返回key
          const localVersionKey = overturnDict[item] // 本地的版本的key
          if (!localVersionKey) {
            return false
          }
          const localVersion = this.version[localVersionKey] || ''
          return newVersion[item] !== localVersion
        })
        // 如果key有值,就调取接口
        if (keys.length) {
          keys.forEach((item: any) => {
            const localVersionKey = overturnDict[item] // 本地的版本的key
            if (localVersionKey) {
              try {
                Api[localVersionKey]().then((res: any) => {
                  if (res.code == 0 || res.code == 10000) {
                    this[localVersionKey] = res.data
                    this.version[localVersionKey] = newVersion[item]
                  }
                })
              } catch (e) {
                console.log('err', e)
              }
            }
          })
        }
      })
    },
  },
})
